package com.pengtu.web.project;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.Department;
import com.pengtu.entity.app.Organization;
import com.pengtu.service.ServiceException;
import com.pengtu.service.app.AccountManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

/**
 * 部门管理
 * 
 * @author libitum
 * 
 */
@Results({ @Result(name = BaseActionSupport.RELOAD, type = "redirect", 
		location = "/project/sign/org/department.action?deptId=${deptId}&projectId=${projectId}&typeId=${typeId}&organizationId=${organizationId}"),
		@Result(name = "deptsuc", type = "redirect", location = "/orgsuccess.jsp")})
public class DepartmentAction extends BaseActionSupport<Department> {

	private static final long serialVersionUID = 1L;

	private AccountManager accountManager;

	// -- 页面属性 --//
	private String id;
	private Department entity;
	private List<Organization> organizationList;
	private Page<Department> page = new Page<Department>(10);// 每页5条记录
	private String organizationId;
	private String orgName;
	private String deptName;
	private Organization organization;
	private String deptId;
	private String projectId;
	private String typeId;
	private String signid;
	public String getSignid() {
		return signid;
	}

	public void setSignid(String signid) {
		this.signid = signid;
	}

	@Action("/project/sign/org/department")
	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		QueryFilter queryfilters = QueryFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		if("1".equals(typeId)){
			queryfilters.addFilter("organization.id", organizationId);
		}else{
			if(StringUtils.isNotEmpty(deptId)){
				queryfilters.addFilter("fid", deptId);
			}else{
				queryfilters.addFilter("organization.id", organizationId);
			}
		}
		if (!page.isOrderBySetted()) {
			page.setOrderBy("createDate");
			page.setOrder(Page.DESC);
		}
		page = accountManager.findPageDept(page, queryfilters);
		return SUCCESS;
	}
	
	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(deptId)) {
			//如果是建设单位的话，不添加父部门
			if(typeId.equals("1")){
				Organization orgg = accountManager.getOrganization(deptId);
				if(null!=orgg)
					entity.setOrganization(orgg);
			}else{
				Department depp = accountManager.getDepartment(deptId);
				if(null!=depp)
					entity.setOrganization(depp.getOrganization());
			    entity.setFid(deptId);
			}
		} else {
			//部门所属单位
			Organization org = accountManager.getOrganization(Struts2Utils.getParameter("organizationId"));
			if(null!=org)
				entity.setOrganization(org);
		}
		accountManager.saveDepartment(entity);
		String mes = "";
		if(StringUtils.isNotEmpty(id)){
			mes = "修改";
		}else{
			mes = "新增";
		}
		addActionMessage(mes + "部门"+entity.getDepName());
		deptName = entity.getDepName();
		if (StringUtils.isNotEmpty(projectId)){
			//如果是建设单位,按照单位id获取部门列表
			if("1".equals(typeId)){
				deptId = entity.getOrganization().getId();
			}else{
				//根据父部门的Id号获取子部门
				deptId = entity.getFid();
			}
			return "successMessage";
		}
		Struts2Utils.getSession().setAttribute("deptId", entity.getId());
		Struts2Utils.getSession().setAttribute("depName", entity.getDepName());
		return "deptsuc";
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		try {
			accountManager.deleteDepartment(id);
			addActionMessage("删除部门成功");
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			addActionMessage("删除部门失败");
		}
		return RELOAD;
	}
	
	public void deleteDept(){
		addActionMessage("删除Id为"+id+"的部门!");
		accountManager.deleteDepartment(id);
		Struts2Utils.renderText("1");
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isNotEmpty(id)) {
			entity = accountManager.getDepartment(id);
		} else {
			entity = new Department();
		}
	}

	/**
	 * 支持使用Jquery.validate Ajax检验用户名是否重复.
	 */
	public String checkDepCode() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String newDepCode = request.getParameter("depCode");
		String oldDepCode = request.getParameter("oldDepCode");

		if (accountManager.isDepCodeUnique(newDepCode, oldDepCode)) {
			Struts2Utils.renderText("true");
		} else {
			Struts2Utils.renderText("false");
		}
		// 因为直接输出内容而不经过jsp,因此返回null.
		return null;
	}

	public String getDeptName() {
		return deptName;
	}

	public Organization getOrganization() {
		return organization;
	}

	public String getOrganizationId() {
		return organizationId;
	}

	public void setOrganizationId(String organizationId) {
		this.organizationId = organizationId;
	}

	public String getOrgName() {
		return orgName;
	}

	public void setOrgName(String orgName) {
		this.orgName = orgName;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Department getEntity() {
		return entity;
	}

	public void setEntity(Department entity) {
		this.entity = entity;
	}

	public Page<Department> getPage() {
		return page;
	}

	public void setPage(Page<Department> page) {
		this.page = page;
	}

	public AccountManager getAccountManager() {
		return accountManager;
	}

	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

	public List<Organization> getOrganizationList() {
		return organizationList;
	}

	@Override
	public Department getModel() {
		// TODO Auto-generated method stub
		return entity;
	}

	public String getDeptId() {
		return deptId;
	}

	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}

	public String getProjectId() {
		return projectId;
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	public String getTypeId() {
		return typeId;
	}

	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}
}
